home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************
-
- File: ResourceUtils.cp
-
- Copyright © 1996, 1997, 1998 Apple Computer, Inc., All Rights Reserved
-
-
- You may incorporate this sample code into your applications without
- restriction, though the sample code has been provided "AS IS" and the
- responsibility for its operation is 100% yours. However, what you are
- not permitted to do is to redistribute the source as "DSC Sample Code"
- after having made changes. If you're going to re-distribute the source,
- we require that you make it clear in the source that the code was
- descended from Apple Sample Code, but that you've made changes.
-
- *************************************************************************************/
-
- #include "String_Utils.h"
- #include "ResourceUtils.h"
- #include "Common.h"
-
- #ifndef __MACTYPES__
- #include <MacTypes.h>
- #endif
-
- #ifndef __TEXTUTILS__
- #include <TextUtils.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __ICONS__
- #include <Icons.h>
- #endif
-
-
- void Resource_GetIndString(short inResourceId, UInt32 inIndex, SInt16 inDestSize, StringPtr outString, OSStatus *error)
- {
- WARNING(outString != nil, "Resource_GetIndString outString was nil");
- WARNING(error != nil, "Resource_GetIndString error was nil");
- WARNING(inDestSize > 0, "Resource_GetIndString inDestSize was <= 0");
- WARNING(inIndex <= 0x7fff, "Resource_GetIndString inIndex to large");
- WARNING(inIndex >= 0x0001, "Resource_GetIndString inIndex to small");
-
- *error = noErr; // assume no error
-
- Str255 loadedString; // the string we loaded from a resource
-
- // load the string
- GetIndString(loadedString, inResourceId, inIndex);
- WARNING(loadedString[0] != 0, "Resource_GetIndString failed to get a string");
-
- // check and return an error
- if (loadedString[0] == 0)
- {
- outString[0] = 0;
- *error = memFullErr;
- return;
- }
-
- // copy the string
- CopyPStr(loadedString, outString, inDestSize);
- }
-
-
- Handle Resource_Get1IconSuite(short inResourceID, IconSelectorValue inSelector, OSStatus *error)
- {
- const UInt32 kNumIconTypes = 9;
- const OSType iconResourceTypes[kNumIconTypes] =
- { 'ICN#', 'icl4', 'icl8',
- 'ics#', 'ics4', 'ics8',
- 'icm#', 'icm4', 'icm8' }; // these res types also enumerated in ISpUserInterface.cp
- const IconSelectorValue iconSelectors[kNumIconTypes] =
- { svLarge1Bit, svLarge4Bit, svLarge8Bit,
- svSmall1Bit, svSmall4Bit, svSmall8Bit,
- svMini1Bit, svMini4Bit, svMini8Bit };
-
- Handle iconHandles[kNumIconTypes] = { nil, nil, nil,
- nil, nil, nil,
- nil, nil, nil };
-
- Handle theIconSuite = nil; // the icon suite we are trying to create
- UInt32 iconTypeItr; // use to loop through the different members of the suite
- OSType type; // type for this iteration
- Handle handle; // handle for this iteration
- Boolean found = false; // true if we have found any icons to add to the suite
-
- // get and clean the resources
- for(iconTypeItr = 0; iconTypeItr < kNumIconTypes; iconTypeItr++)
- {
- type = iconResourceTypes[iconTypeItr];
- handle = Get1Resource(type, inResourceID);
-
- // if we don't want this selector skip it
- if (!(iconSelectors[iconTypeItr] & inSelector)) { continue; }
-
- if (handle)
- {
- Resource_CleanResHandle(handle);
- iconHandles[iconTypeItr] = handle;
-
- found = true;
- }
- }
-
- // == found nothing return nil & resNotFound ==
- if (!found)
- {
- *error = resNotFound;
- return nil;
- }
-
- // == create a new icon suite ==
- *error = NewIconSuite(&theIconSuite);
-
- // == handle that error if required ==
- if (*error)
- {
- for(iconTypeItr = 0; iconTypeItr < kNumIconTypes; iconTypeItr++)
- {
- handle = iconHandles[iconTypeItr];
-
- if (handle != nil) { DisposeHandle(handle); }
- }
-
- return nil;
- }
-
- // == add the icons to the suite ==
- for(iconTypeItr = 0; iconTypeItr < kNumIconTypes; iconTypeItr++)
- {
- type = iconResourceTypes[iconTypeItr];
- handle = iconHandles[iconTypeItr];
-
- if (handle != nil)
- {
- OSErr harmlessError; // only error is paramErr which we shouldn't get
-
- harmlessError = AddIconToSuite(handle, theIconSuite, type);
- WARNING(harmlessError == noErr, "Resource_Get1IconSuite AddIconToSuite (paramErr?)");
- }
- }
-
- return theIconSuite;
- }
-
- void Resource_CleanResHandle(Handle inResourceHandle)
- {
- // step 1 detach
- DetachResource(inResourceHandle);
- RES_WARNING("Resource_CleanResHandle DetachResource failed");
-
- // step 2 clean up the handle
- HNoPurge(inResourceHandle);
- RES_WARNING("Resource_CleanResHandle HNoPurge failed");
-
- // assert success
- WARNING(*inResourceHandle != nil, "ResourceToCleanHandle ended up with an empy handle!");
- }
-
- void Resource_Delete1(ResType inType, short inID, OSStatus *err)
- {
- *err = noErr;
-
- // get the resource and error check
- Handle resHandle = Get1Resource(inType, inID);
- *err = ResError();
- if ((*err == noErr) && (resHandle == nil)) { *err = resNotFound; }
- if (*err) { return; }
-
- RemoveResource(resHandle);
- RES_WARNING("Resource_Delete1 RemoveResource failed");
-
- DisposeHandle(resHandle);
- RES_WARNING("Resource_Delete1 DisposeHandle failed");
- }
-
-
- void Resource_GetString(short inResourceId, SInt16 inDestSize, StringPtr outString, OSStatus *error)
- {
- WARNING(outString != nil, "Resource_GetString outString was nil");
- WARNING(error != nil, "Resource_GetString error was nil");
- WARNING(inDestSize > 0, "Resource_GetString inDestSize was <= 0");
-
- *error = noErr; // assume no error
-
- StringHandle aStringH; // the string handle we loaded from a resource
-
- aStringH = GetString(inResourceId);
- WARNING(aStringH != nil, "Resource_GetString failed!");
-
- if (aStringH == nil)
- {
- outString[0] = 0;
- *error = memFullErr;
- return;
- }
-
- // copy the string
- CopyPStr(*aStringH, outString, inDestSize);
-
- // release the resource
- ReleaseResource( (Handle) aStringH);
- }
-
-
- void Resource_Copy(short inFromRef, short inFromID, ResType inType, short inToRef, short inToID, OSStatus *error)
- {
- StCurResFileState stackResFileSaver;
-
- Handle theResource;
-
- // use the source ref
- UseResFile(inFromRef);
- *error = ResError();
- if (*error != noErr) { return; }
-
- // get the resource
- theResource = Get1Resource(inType, inFromID);
- *error = ResError();
- if (theResource == nil) { *error = resNotFound; }
- if (*error != noErr) { return; }
-
- // get the name from the resource info
- Str255 name;
-
- short ignoredID;
- ResType ignoredType;
-
- GetResInfo(theResource, &ignoredID, &ignoredType, name);
-
- // detacht this resource from that file
- Resource_CleanResHandle(theResource);
-
- // use the destinatino resource file
- UseResFile(inToRef);
- *error = ResError();
- if (*error != noErr)
- {
- DisposeHandle(theResource);
- return;
- }
-
- // add this resource
- AddResource(theResource, inType, inToID, name);
- *error = ResError();
-
- // release this resource
- ReleaseResource(theResource);
- *error = ResError();
- }
-
-
- // ===========================================================================
- // StCurResFileState
- // ===========================================================================
-
- StCurResFileState::StCurResFileState(void)
- {
- Save();
- }
-
- StCurResFileState::StCurResFileState(short inNewCurResFile)
- {
- Save();
- UseResFile(inNewCurResFile);
- }
-
- StCurResFileState::~StCurResFileState()
- {
- Restore();
- }
-
-
- void
- StCurResFileState::Save(void)
- {
- mResFileRefNum = CurResFile();
- }
-
-
- void
- StCurResFileState::Restore()
- {
- UseResFile(mResFileRefNum);
- }
-
-
-